package servlet;

import dao.GoodsDao;
import dao.UserDao;
import model.Goods;
import model.User;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = "/EditGoodsServlet.do")
public class EditGoodsServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String idParam = req.getParameter("id");
        System.out.println(idParam);
        if (idParam == null || idParam.isEmpty()) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "User ID is required");
            return;
        }

        try {
            int Goods_Id = Integer.parseInt(idParam);

            UserDao userDao=new UserDao();
             Goods goods =userDao.getGoodsByGOODSId(Goods_Id);
            System.out.println(goods.getGoods_id());
            System.out.println(goods.getGoods_info());
            System.out.println(goods.getGoods_category());
            System.out.println(goods.getGoods_price());


            if (goods != null) {
                HttpSession session = req.getSession();
                session.setAttribute("goods", goods);
                resp.sendRedirect(req.getContextPath() + "/background1/admin/EidtGoodsServlet.jsp");
            } else {
                resp.sendError(HttpServletResponse.SC_NOT_FOUND, "User not found");
            }
        } catch (NumberFormatException e) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid User ID format");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String idParam = req.getParameter("goods_id");
        if (idParam == null || idParam.isEmpty()) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Goods ID is required");
            return;
        }

        try {
            int goodsId = Integer.parseInt(idParam); // 修改这里，使用 idParam 转换为整数
            String goodsInfo = req.getParameter("goods_info");
            String goodsCategory = req.getParameter("goods_category");
            double goodsPrice = Double.parseDouble(req.getParameter("goods_price"));

            Goods updatedGoods = new Goods();
            updatedGoods.setGoods_id(goodsId);
            updatedGoods.setGoods_info(goodsInfo);
            updatedGoods.setGoods_category(goodsCategory);
            updatedGoods.setGoods_price(goodsPrice);

            GoodsDao goodsDao = new GoodsDao();
            boolean isUpdated = goodsDao.updateGoods(updatedGoods);

            if (isUpdated) {
                resp.sendRedirect(req.getContextPath() + "/background1/admin/goodsManager.jsp");
            } else {
                resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Goods update failed");
            }
        } catch (NumberFormatException e) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid goods ID format or goods price format");
        }
    }


}
